﻿Imports System.IO

Public Class Form3
#Region "Declare"
    Public ListofPopup As New ArrayList
    Dim TimerWatch As Integer = 0
    Dim XofScreen As Integer = Screen.PrimaryScreen.Bounds.Width
    Dim YofScreen As Integer = Screen.PrimaryScreen.Bounds.Height
    Dim NameList As New ArrayList
    Dim Excluded As New ArrayList
    Dim RandomSecondsCount As Integer = 12
    Dim FireCount As Integer = 1
    Dim AddTime As Integer = 2
    Dim PriceClick As Integer = 0
    Dim ExcludedNumber As New ArrayList
#End Region
#Region "Op"
    Public Sub ShowFireWorks(NumberofFireWorks As Integer)
        Try
            For value As Integer = 1 To NumberofFireWorks
                SyncLock Me
                    Dim RandX As New Random
                    Dim XLoc As Integer = RandX.Next(1, XofScreen - 476)
                    Dim RandY As New Random
                    Dim YLoc As Integer = RandX.Next(1, YofScreen - 300)

                    Dim popup As New Popup
                    popup.Name = ListofPopup.Count
                    popup.parent = Me
                    popup.Location = New Point(XLoc, YLoc)
                    popup.Height = 1
                    popup.Width = 1
                    popup.Show()
                    ListofPopup.Add(popup)
                End SyncLock
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            UnloadPopUps()
        End Try
    End Sub
    Sub UnloadPopUps()
        Try
            If ListofPopup.Count > 0 Then
                For Each Formed As Popup In ListofPopup
                    Formed.PictureBox2.Image = Nothing
                    Formed.Hide()
                    Formed.CloseMe()
                Next
                ListofPopup.Clear()
            End If
        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try

    End Sub
    Sub Start()
        Try
            ResetTemp()
            If NameList.Count <> 0 Then
                Timer1.Enabled = True
                Dim Rando As New Random
                'RandomSecondsCount = Rando.Next(4, 13)
                RandomSecondsCount = AutoFireCount() + AddTime
                UnloadPopUps()
                Timer3.Enabled = False
                BlinkNumer.Enabled = False
                Label1.ForeColor = Color.DarkRed
                lblPrice.ForeColor = Color.DarkRed
                TimerWatch = 0
            Else
                MsgBox("No Names in the list", MsgBoxStyle.Critical)
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
        
    End Sub
    Function PickRandom() As String
        Try
SelectAgain:
            Dim rand As New Random
            Dim ran As Integer = rand.Next(0, NameList.Count)
            Dim ReturnString As String = NameList(ran).ToString.Replace(Chr(10), "")
            For Each item In Excluded
                If ReturnString = item Then
                    GoTo SelectAgain
                End If
            Next
            'Excluded.Add(ReturnString)
            Return ReturnString
        Catch ex As Exception
            Return ex.Message
        End Try
        Return Nothing
    End Function
    Sub AddExcludedonTextFile(Name)
        Try
            Dim FILE_NAME As String = My.Application.Info.DirectoryPath & "\excluded.txt"
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
            objWriter.WriteLine(Name)
            objWriter.Close()
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical)
        End Try
    End Sub
    Sub AddExcludedNumberOnTextFile(Name)
        Try
            Dim FILE_NAME As String = My.Application.Info.DirectoryPath & "\excludedNumber.txt"
            Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)
            objWriter.WriteLine(Name)
            objWriter.Close()
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical)
        End Try
    End Sub
    Function AutoFireCount() As Integer
        Dim ReturnCount As Integer = 1
        Try
            Dim equation As Double = Excluded.Count / NameList.Count
            Dim Percent As Double = equation * 10
            If Percent < 1 Then
                ReturnCount = 1
            Else
                ReturnCount = Convert.ToInt32(Percent)
            End If
        Catch ex As Exception
            ReturnCount = 1
        End Try
        Return ReturnCount
    End Function
    Sub ShowSelfie()
        Try
            PriceClick = 0
            'GeneratePrice()
            If File.Exists(My.Application.Info.DirectoryPath & "\Pictures\" & Label1.Text & ".jpg") = True Then
                PictureBox1.ImageLocation = My.Application.Info.DirectoryPath & "\Pictures\" & Label1.Text & ".jpg"
            Else
                PictureBox1.ImageLocation = My.Application.Info.DirectoryPath & "\Pictures\default.png"
            End If
        Catch ex As Exception
            PictureBox1.ImageLocation = My.Application.Info.DirectoryPath & "\Pictures\default.png"
        End Try

    End Sub
    Sub ResetTemp()
        Try
            lblPrice.Text = "-"
            PictureBox1.ImageLocation = My.Application.Info.DirectoryPath & "\Pictures\default.png"
        Catch ex As Exception
            PictureBox1.ImageLocation = My.Application.Info.DirectoryPath & "\Pictures\default.png"
        End Try
    End Sub
    Sub CheckRemaining()
        Try
            lblRemaining.Text = "Remaining : " & (NameList.Count - Excluded.Count)
        Catch ex As Exception
            lblRemaining.Text = "Remaining : --"
        End Try
    End Sub
    Sub GeneratePrice()
        If NameList.Count <= 0 Then
            MsgBox("No Names in the list", MsgBoxStyle.Critical)
            Exit Sub
        End If
        PriceClick += 1
        If PriceClick = 1 Then
            If ExcludedNumber.Count < NameList.Count Then
                Try
SelectAgain:
                    Dim rand As New Random
                    Dim ran As Integer = rand.Next(1, NameList.Count)
                    Dim ReturnString As String = ran.ToString
                    For Each item In ExcludedNumber
                        If ReturnString = item Then
                            GoTo SelectAgain
                        End If
                    Next

                    ExcludedNumber.Add(ReturnString)
                    lblPrice.Text = ReturnString
                    AddExcludedNumberOnTextFile(lblPrice.Text)
                Catch ex As Exception
                    'lblPrice
                    MsgBox(ex.Message)
                End Try
            Else
                MsgBox("No More Prices", vbInformation)
            End If
        ElseIf PriceClick <= 2 Then
            Dim Quest As MsgBoxResult = MsgBox("Unable to give more prices to the winner, Click OK to Generate a new winner", MsgBoxStyle.OkCancel)
            If Quest = MsgBoxResult.Ok Then
                ResetTemp()
                Timer3.Enabled = False
                Timer1.Enabled = False
                Timer2.Enabled = False
                Label1.ForeColor = Color.DarkRed
                TimerWatch = 0
                RumbleTimer.Enabled = True
                lblPrice.Text = "-"
            End If
            'ElseIf PriceClick <= 3 Then
            '    MsgBox("Unable to skip this number.", vbInformation)
        End If
    End Sub
    Sub AfterGenerate()
        Excluded.Add(Label1.Text)
        Timer1.Enabled = False
        Timer2.Enabled = False
        'ShowFireWorks(FireCount)
        'Show Selfie
        ShowSelfie()
        CheckRemaining()
        GenerateRumbleNumber()
        Timer3.Enabled = True
        AddExcludedonTextFile(Label1.Text)
    End Sub
#End Region

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim OpenFile As New OpenFileDialog
        OpenFile.Filter = ".txt|*.txt"
        Dim result As DialogResult = OpenFile.ShowDialog()
        Try
            NameList.Clear()
            Dim path As String = OpenFile.FileName
            ' Read in text.
            Dim text As String = File.ReadAllText(path).Trim
            Dim listtext As String() = text.Split(vbCrLf)
            For Each item In listtext
                If item <> "" Then
                    NameList.Add(item)
                End If
            Next
            CheckRemaining()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    
    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
        Try
            If Excluded.Count - 1 >= NameList.Count Then
                'MsgBox("All Names has been called")
                Label1.Text = "All Names has been called, Congratualtions to all the winners!"
                Timer1.Enabled = False
                Timer2.Enabled = False
                Timer3.Enabled = False
                TimerWatch = 100
            Else
                Label1.Text = PickRandom()
                'ShowSelfie()
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            TimerWatch += 1
            Timer2.Enabled = True
            Timer2.Interval = TimerWatch * 50
            'Win!
            Try
                If TimerWatch >= RandomSecondsCount Then
                    AfterGenerate()
                End If
            Catch ex As Exception

            End Try
        Catch ex As Exception

        End Try
    End Sub
    
    Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
        If Label1.ForeColor = Color.Red Then
            Label1.ForeColor = Color.DarkRed
        Else
            Label1.ForeColor = Color.Red
        End If
    End Sub
    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Dim CommandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Application.CommandLineArgs
            For i As Integer = 0 To CommandLineArgs.Count - 1
                'MessageBox.Show(CommandLineArgs(i))
                If CommandLineArgs(i).Contains("AddTime") Then
                    AddTime = CommandLineArgs(i + 1)
                ElseIf CommandLineArgs(i).Contains("help") Then
                    MsgBox("Arguments -AddTime (Int)", vbInformation, "Help")
                End If
            Next

        Catch ex As Exception

        End Try
        Try
            If File.Exists(My.Application.Info.DirectoryPath & "\excluded.txt") Then
                Dim text As String = File.ReadAllText(My.Application.Info.DirectoryPath & "\excluded.txt")
                Dim listtext As String() = text.Split(vbCrLf)
                For Each item In listtext
                    Excluded.Add(item.ToString.Replace(Chr(10), ""))
                Next
            End If

            If File.Exists(My.Application.Info.DirectoryPath & "\excludedNumber.txt") Then
                Dim text As String = File.ReadAllText(My.Application.Info.DirectoryPath & "\excludedNumber.txt")
                Dim listtext As String() = text.Split(vbCrLf)
                For Each item In listtext
                    ExcludedNumber.Add(item.ToString.Replace(Chr(10), ""))
                Next
            End If

            If Directory.Exists(My.Application.Info.DirectoryPath & "\Pictures") = False Then
                Directory.CreateDirectory(My.Application.Info.DirectoryPath & "\Pictures")
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub


    Private Sub RumbleTimer_Tick(sender As Object, e As EventArgs) Handles RumbleTimer.Tick
        Try
            If Excluded.Count - 1 >= NameList.Count Then
                'MsgBox("All Names has been called")
                Label1.Text = "All Names has been called, Congratualtions to all the winners!"
                RumbleTimer.Enabled = False
            Else
                Label1.Text = PickRandom()
                'ShowSelfie()
            End If
        Catch ex As Exception

        End Try
    End Sub


    Private Sub Label1_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseClick
        'PictureBox1.Visible = True
        FireCount = AutoFireCount()
        If e.Button = MouseButtons.Right Then

            Start()
            RumbleTimer.Enabled = False
        Else
            ResetTemp()
            Timer3.Enabled = False
            Timer1.Enabled = False
            Timer2.Enabled = False
            BlinkNumer.Enabled = False
            RumbleNumberShow.Enabled = False
            RumbleNumberStop.Enabled = False

            Label1.ForeColor = Color.DarkRed
            lblPrice.ForeColor = Color.DarkRed
            TimerWatch = 0
            RumbleTimer.Enabled = True
        End If
    End Sub

    
    Private Sub lblPrice_MouseDown(sender As Object, e As MouseEventArgs) Handles lblPrice.MouseDown
        'GeneratePrice()
    End Sub

    Sub GenerateRumbleNumber()
        RumbleNumberShow.Enabled = True
        RumbleNumberStop.Enabled = True
    End Sub
    Private Sub RumbleShow_Tick(sender As Object, e As EventArgs) Handles RumbleNumberShow.Tick
        Dim rand As New Random
        Dim ran As Integer = rand.Next(1, NameList.Count)
        Dim ReturnString As String = ran.ToString
        lblPrice.Text = ReturnString
        'lblPrice.ForeColor = Color.White
    End Sub
    Dim RumbleNumber As Integer = 0
    Private Sub RumbleNumberStop_Tick(sender As Object, e As EventArgs) Handles RumbleNumberStop.Tick
        RumbleNumber += 1
        If RumbleNumber = 5 Then
            RumbleNumberShow.Enabled = False
            RumbleNumberStop.Enabled = False
            BlinkNumer.Enabled = True
            RumbleNumber = 0
            GeneratePrice()
            'lblPrice.ForeColor = Color.DarkRed
        End If
    End Sub

    Private Sub BlinkNumer_Tick(sender As Object, e As EventArgs) Handles BlinkNumer.Tick
        If lblPrice.ForeColor = Color.Red Then
            lblPrice.ForeColor = Color.DarkRed
        Else
            lblPrice.ForeColor = Color.Red
        End If
    End Sub

    
End Class